﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDataBaseWork;
using XF.Meeting.Entity.Query;
using XF.Meeting.IDal;

namespace XF.Meeting.DAL
{
    public class ReportDal:BaseDAL, IReportDal
    {
        /// <summary>
        ///获取会议室统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int)> GetMeetingRoomReport(ReportQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",query.RID),
                  new SqlParameter("@CRoomID",query.CRoomID),
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@BeginTime",query.BeginTime),
                  new SqlParameter("@EndTime",query.EndTime),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetCRoomReport", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetCRoomReport EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            
            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount));
        }

        /// <summary>
        ///获取人员统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int)> GetPersonMeetingReport(ReportQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",query.RID),               
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@BeginTime",query.BeginTime),
                  new SqlParameter("@EndTime",query.EndTime),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetPersonMeetingReport", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetPersonMeetingReport EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount));
        }

        /// <summary>
        ///获取部门统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int)> GetDeptMeetingReport(ReportQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",query.RID),
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@BeginTime",query.BeginTime),
                  new SqlParameter("@EndTime",query.EndTime),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetDeptMeetingReport", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetDeptMeetingReport EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount));
        }

        /// <summary>
        ///获取部门人员统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int)> GetDeptPersonMeetingReport(ReportQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",query.RID),
                  new SqlParameter("@BeginTime",query.BeginTime),
                  new SqlParameter("@EndTime",query.EndTime),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetDeptPersonMeetingReport", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetDeptPersonMeetingReport EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount));
        }

        /// <summary>
        ///获取人员统计详情报表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int)> GetPersonMeetingDetailsReport(ReportQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserID",query.UserID),
                  new SqlParameter("@BeginTime",query.BeginTime),
                  new SqlParameter("@EndTime",query.EndTime),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetPersonMeetingDetailsReport", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetPersonMeetingDetailsReport EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount));
        }
    }
}
